<template>
 <div class="exhibition-contailer">
    <basic-container>
          <h4>住院治疗情况</h4>
          <avue-crud ref="zyzlcrud" v-model="zyzl.form" 
                    :data="zyzl.tableData" 
                    :option="zyzl.tableOption" 
                    :table-loading="zyzl.tableLoading"  
                    @row-save="zyzlHandleAdd"
                    @row-update="zyzlHandleEdit"
                    @row-del="zyzlHandleDel"
                    @refresh-change="zyzlRefreshChange">
          </avue-crud>
    </basic-container>
    <basic-container>
          <h4>主要用药情况</h4>
          <avue-crud ref="yyqkcrud" v-model="yyqk.form" 
                    :data="yyqk.tableData" 
                    :option="yyqk.tableOption" 
                    :table-loading="yyqk.tableLoading" 
                    @row-save="yyqkHandleAdd"
                    @row-update="yyqkHandleEdit"
                    @row-del="yyqkHandleDel" 
                    @refresh-change="yyqkRefreshChange">
          </avue-crud>
    </basic-container>
    <basic-container>
          <h4>非免疫规划预防接种史</h4>
          <avue-crud ref="jzscrud" v-model="jzs.form" 
                    :data="jzs.tableData" 
                    :option="jzs.tableOption" 
                    :table-loading="jzs.tableLoading"  
                    @refresh-change="jzsRefreshChange"
                    @row-save="jzsHandleAdd"
                    @row-update="jzsHandleEdit"
                    @row-del="jzsHandleDel">
          </avue-crud>
    </basic-container>
    <basic-container>
      <avue-form :option="option" v-model="form" @submit="submit">
      </avue-form>
    </basic-container>
</div>
</template>
<script>
import {zyzlTableOption,yyqkTableOption,jzsTableOption,fromOption} from '@/const/cust/health/other'
import {findObj,saveObj,findZyList,delZyObj,addZyObj,updateZyObj,
        findYyqkList,delYyqkObj,addYyqkObj,updateYyqkObj,
        findJzsList,delJzsObj,addJzsObj,updateJzsObj} from '@/api/cust/health/health'
export default {
  data() {
    return {
      jktjid:null,
      grjbxxid:null,
      form: {
        jktjid:null,
        grjbxxid:null,
      },
      option: fromOption,
      queryParam: {
        start: 0,
        length: 100,
        jktjid: null,//体检id
        grjbxxid: null,//个人基本信息id
      },
      zyzl:{
        tableLoading: false,
        tableOption: zyzlTableOption,
        tableData:[],      
      },
      yyqk:{
        tableLoading: false,
        tableOption: yyqkTableOption,
        tableData:[],
        form:{},
      },
      jzs:{
        tableLoading: false,
        tableOption: jzsTableOption,
        tableData:[],
        form:{},
      },
      
    }
  },
  watch: {
    'form.jkpjnr'(){
      this.colDisplay('jkpjnr','jkpjnrDes', "2",'健康评价异常说明');
    },
    'form.jkzdwxyskzff'(){
      this.colDisplay('jkzdwxyskzff','wxyskzjtzmb', "5",'减体重目标(kg)');
      this.colDisplay('jkzdwxyskzff','wxyskzjyjzym', "6",'建议接种疫苗');
      this.colDisplay('jkzdwxyskzff','wxyskzDes', "7",'其他说明');
    }
  },
  computed: {},
  created() {
    this.grjbxxid = this.$route.query.grjbxxid;
    this.jktjid = this.$route.params.id;
    this.queryParam.grjbxxid = this.grjbxxid;
    this.queryParam.jktjid = this.jktjid;
    this.form.grjbxxid = this.grjbxxid;
    this.form.jktjid = this.jktjid;
    this.getTjb();
    this.getZyList();
    this.getYyqkList();
    this.getJzsList();
  },
  methods: {
    //住院治疗情况列表刷新
    zyzlRefreshChange() {
      this.getZyList();
    },
    yyqkRefreshChange() {
      this.getYyqkList();
    },
    jzsRefreshChange(){
      this.getJzsList();
    },
    //住院治疗情况添加
    zyzlHandleAdd: function (row, done) {  
      row.jktjid=this.jktjid;
      row.grjbxxid=this.grjbxxid;
      addZyObj(row).then(res => {
        this.getZyList();
        this.$message({
          showClose: true,
          message: "添加成功",
          type: "success"
        });
        done();
      });
    },
     //编辑
    zyzlHandleEdit (row, index, done) {
      updateZyObj(row).then(res => {
        this.getZyList();
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success"
        });
        done();
      });
    },
    //删除
    zyzlHandleDel (row, index) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delZyObj(row.id).then(() => {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.getZyList();
          })
        })
        .catch(function() {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'success'
          })
        })
    },
    yyqkHandleAdd: function (row, done) {  
      row.jktjid=this.jktjid;
      row.grjbxxid=this.grjbxxid;
      addYyqkObj(row).then(res => {
        this.getYyqkList();
        this.$message({
          showClose: true,
          message: "添加成功",
          type: "success"
        });
        done();
      });
    },
     //编辑
    yyqkHandleEdit (row, index, done) {
      updateYyqkObj(row).then(res => {
        this.getYyqkList();
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success"
        });
        done();
      });
    },
    //删除
    yyqkHandleDel (row, index) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delYyqkObj(row.id).then(() => {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.getYyqkList();
          })
        })
        .catch(function() {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'success'
          })
        })
    },
    //添加非免疫规划预防接种史
    jzsHandleAdd: function (row, done) {  
      row.jktjid=this.jktjid;
      row.grjbxxid=this.grjbxxid;
      addJzsObj(row).then(res => {
        this.getJzsList();
        this.$message({
          showClose: true,
          message: "添加成功",
          type: "success"
        });
        done();
      });
    },
     //编辑
    jzsHandleEdit (row, index, done) {
      updateJzsObj(row).then(res => {
        this.getJzsList();
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success"
        });
        done();
      });
    },
    //删除
    jzsHandleDel (row, index) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delJzsObj(row.id).then(() => {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.getJzsList();
          })
        })
        .catch(function() {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'success'
          })
        })
    },
    submit(){
      saveObj(this.form).then(data => {
        this.from=data.data;
        this.$message({
          showClose: true,
          message: "保存成功",
          type: "success"
        });
      });
    },
    //获取健康体检表数据
    getTjb(){
      findObj(this.form).then(res => {
        if(res.data.code == 0 && res.data.data != null){
          this.form = res.data.data[0];
        }
      })
    },
    //获取住院治疗情况
    getZyList() {
      this.zyzl.tableLoading = true
      findZyList(this.queryParam).then(res => {
        this.zyzl.tableLoading = false
        this.zyzl.tableData = res.data.data
      })
    },
    //获取主要用药情况
    getYyqkList() {
      this.yyqk.tableLoading = true
      findYyqkList(this.queryParam).then(res => {
        this.yyqk.tableLoading = false
        this.yyqk.tableData = res.data.data
      })
    },
    //获取非免疫规划预防接种史
    getJzsList() {
      this.jzs.tableLoading = true
      findJzsList(this.queryParam).then(res => {
        this.jzs.tableLoading = false
        this.jzs.tableData = res.data.data
      })
    },
    colDisplay(key1,key2, value, message) {
      var flag = false
      const des = this.findColumnIndex(key2);
      const desCol = this.option.column[des];
      const data = this.validatenull(this.form[key1])
      ? this.form[key1]
      : this.form[key1].split(',')
      flag = Array.isArray(data)
        ? data.includes(value)
        : this.form[key1] === value
      if (flag) {
        desCol.display = true
        desCol.rules = [
          {
            required: true,
            message: '请输入' + message,
            trigger: "blur",
          }
        ]
      } else { 
        desCol.display = false
        desCol.rules = []
      }
    },
    findColumnIndex(prop) {
      let formIndex = -1
      this.option.column.forEach((ele, index) => {        
        if (ele.prop === prop) {
          formIndex = index
        }
      })
      return formIndex;
    },

  }
}
</script>